//
//  LYRollView.m
//  SelectCity
//
//  Created by liuyan on 2019/3/17.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYRollView.h"
#import "LYRollContentView.h"

@interface LYRollView ()

@property (nonatomic, strong) NSTimer *timer;
/**
 *  两个子view轮播出现
 */
@property (nonatomic, weak) LYRollContentView *contentView1;
@property (nonatomic, weak) LYRollContentView *contentView2;
/**
 *  当前轮播的是哪一条
 */
@property (nonatomic, assign) int currentIndex;

@end

@implementation LYRollView

- (void)setTitleArr:(NSArray *)titleArr {
    _titleArr = titleArr;
    
    [self.timer invalidate];
    if (titleArr.count > 0) {
        self.currentIndex = 0;
        self.contentView1.text = self.titleArr[self.currentIndex];
        if (titleArr.count > 1) {
            self.contentView2.text = self.titleArr[self.currentIndex + 1];
            self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollToNext) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        }
    } else {
        self.contentView1.text = @"";
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        LYRollContentView *contentView1 = [[LYRollContentView alloc] init];
        [contentView1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTap:)]];
        self.contentView1 = contentView1;
        [self addSubview:contentView1];
        LYRollContentView *contentView2 = [[LYRollContentView alloc] init];
        [contentView2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTap:)]];
        self.contentView2 = contentView2;
        [self addSubview:contentView2];
        
        self.clipsToBounds = YES;
        self.currentIndex = 0;
        self.titleArr = [NSArray array];
    }
    return self;
}

- (void)scrollToNext {
    CGSize size = self.bounds.size;
    [UIView animateWithDuration:1 animations:^{
        self.contentView1.frame = CGRectOffset(self.contentView1.frame, 0, -size.height*0.5);
        self.contentView2.frame = CGRectOffset(self.contentView2.frame, 0, -size.height*0.5);
    } completion:^(BOOL finished) {
        self.contentView1.frame = CGRectOffset(self.contentView2.frame, 0, size.height*0.5);
        LYRollContentView *buffView = self.contentView1;
        self.contentView1 = self.contentView2;
        self.contentView2 = buffView;
        
        NSInteger count = self.titleArr.count;
        if (count <= 0) {
            return;
        }
        self.currentIndex = (self.currentIndex + 1) % count;
        if (self.titleArr.count > 0) {
            self.contentView1.text = self.titleArr[self.currentIndex % count];
            self.contentView2.text = self.titleArr[(self.currentIndex + 1) % count];
        }
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize size = self.bounds.size;
    self.contentView1.frame = CGRectMake(0, 0, size.width, size.height*0.5);
    self.contentView2.frame = CGRectMake(0, size.height*0.5, size.width, size.height*0.5);
}

- (void)contentViewTap:(UITapGestureRecognizer *)tap {
    if (self.titleArr.count < 1) {
        return;
    }
    
    if ([tap.view isEqual:self.contentView1]) {
        _block(self.currentIndex);
    } else {
        _block((self.currentIndex + 1) % self.titleArr.count);
    }
}

@end
